package sg.edu.nus.iss.club;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Booking {
	
	public static final String DATE_FORMAT = "d-MMM-yyyy";
	public static final String TIME_FORMAT = "d-MMMM-yyyy H:mm";
	
	private Member member;
	private Facility facility;
	private Date startDate;
	private Date endDate;
	private static final DateFormat df = new SimpleDateFormat(TIME_FORMAT);
	
	public Booking(Member member, Facility facility, Date startDate, Date endDate) throws BadBookingException {
		if (member == null) {
			throw new BadBookingException("Member cannot be null");
		} else if (facility == null) {
			throw new BadBookingException("Facility cannot be null");
		} else if (startDate == null || endDate == null) {
			throw new BadBookingException("Start Date or End Date cannot be null");
		} else if (endDate.before(startDate)) {
			throw new BadBookingException("Invalid Date range");
		} else {
			this.member = member;
			this.facility = facility;
			this.startDate = startDate;
			this.endDate = endDate;
		}
	}
	
	public Member getMember() {
		return member;
	}
	public Facility getFacility() {
		return facility;
	}
	public Date getStartDate() {
		return startDate;
	}
	public Date getEndDate() {
		return endDate;
	}
	
	public boolean overlaps(Booking other) {
		if (this.facility.compareTo(other.getFacility()) == 0) {
			return this.overlaps(other.getStartDate(), other.getEndDate());
		} else {
			return false;
		}
	}
	
	public boolean overlaps(Date startDate, Date endDate) {
		return this.startDate.before(endDate) && startDate.before(this.endDate);
	}

	public String toString() {
		return ("Booking: " + facility + " (" + member + "): " + df.format(startDate) + " to " + df.format(endDate));
	}
	
	public void show() {
		System.out.println(this);
	}
	
	

}
